
// utils.js
function formatTime() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();

    // 简单格式化，不使用padStart
    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}


function wakeUp() {
    if (!device.isScreenOn()) {
        // 屏幕关闭则唤醒
        device.wakeUp();
        sleep(2000); // 等待屏幕完全亮起
        console.log("屏幕已唤醒");
    } else {
        console.log("屏幕已经是亮的");
    }
}

function awakenQQ() {
    wakeUp();
    launchApp("QQ"); // 打开QQ com.tencent.mobileqq
    sleep(7000);
    console.log("打开QQ,当前时间：" + formatTime());
    home();
    sleep(1000);
    runtime.accessibilityBridge.getService().performGlobalAction(android.accessibilityservice.AccessibilityService.GLOBAL_ACTION_LOCK_SCREEN);
}

//
function sendMessage(qqNumber, message) {
    wakeUp();
    launchApp("QQ");
    sleep(3000);
    app.startActivity({
        action: "android.intent.action.VIEW",
        data: "mqq://im/chat?chat_type=wpa&version=1&src_type=web&uin=" + qqNumber,
        packageName: "com.tencent.mobileqq",
    });

    sleep(3000);

    var inputBox = className("EditText").findOne(5000);
    if (inputBox) {
        inputBox.setText(message);
        var sendButton = text("发送").findOne(3000) || desc("发送").findOne(3000);
        if (sendButton) {
            sendButton.click();
        }
    }
}


function isClock(clock) {
    var date = new Date();
    var hour = date.getHours();
    if (hour == clock) {
        return random(2000, 52000);
    } else {
        return -1;
    }
}

// 查找可滚动控件并滚动到底部
function swipeToBottom(counts) {
    counts = counts || 3;
    var screenWidth = device.width;
    var screenHeight = device.height;
    // console.log("屏幕尺寸: " + screenWidth + " x " + screenHeight);
    // 多次滑动确保到达底部
    for (var i = 0; i <= counts; i++) {
        // 从屏幕下方向上滑动
        swipe(screenWidth / 2, screenHeight * 0.8, screenWidth / 2, screenHeight * 0.2, 800);
        sleep(1000);
        // 检查是否已经到底（可以检查是否有新的内容加载）
        // 这里可以添加判断是否到达底部的逻辑
    }
    console.log("已完成滑动操作");
}

function simpleClickByBounds(boundsInfo) {
    var left = boundsInfo[0];
    var top = boundsInfo[1];
    var right = boundsInfo[2];
    var bottom = boundsInfo[3];

    var x = Math.floor((left + right) / 2);
    var y = Math.floor((top + bottom) / 2);

    console.log("坐标点击按钮: (" + x + ", " + y + ")");

    // 获取坐标点上的所有元素

    sleep(1000);

    // 执行点击
    var clickResult = click(x, y);
    if (!clickResult) {
        console.log("点击执行失败，坐标是：" + boundsInfo);
        return false;
    } else {
        return true;
    }
}

function clickByBounds(bounds) {
    console.log("坐标点击按钮: (" + bounds.centerX() + ", " + bounds.centerY() + ")");
    // 获取坐标点上的所有元素
    // 执行点击
    var clickResult = click(bounds.centerX(), bounds.centerY());
    if (!clickResult) {
        console.log("点击执行失败，坐标是：" + bounds.centerX() + "," + bounds.centerY());
        return false;
    } else {
        return true;
    }
}


// 导出函数
// 确保正确导出所有函数
module.exports = {
    formatTime: formatTime,
    sendMessage: sendMessage,
    wakeUp: wakeUp,
    isClock: isClock,
    awakenQQ: awakenQQ,
    clickByBounds: clickByBounds,
    swipeToBottom: swipeToBottom,
    simpleClickByBounds: simpleClickByBounds
};

